home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 June: Reference Library / Dev.CD Jun 94.toast / Periodicals / develop / develop Issue 18 / develop 18 code / OSA Sample / Sources / WindowObj.cp < prev    next >
Encoding:
Text File  |  1994-02-04  |  21.4 KB  |  1,111 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        WindowObj.h
  3.  
  4.     Contains:    Windows for a simple Scriptable application.
  5.  
  6.     Developed by:    
  7.         
  8.         Paul G Smith (commstalk hq & Full Moon Software, Inc)
  9.         
  10.         you can leave messages at (UK): 0727 844232; (US): 408 253 7199
  11.         BUT I prefer to be contacted by e-mail
  12.         AppleLink:     SMITH.PG
  13.         Internet:     SMITH.PG@applelink.apple.com
  14.         
  15.         "SimpliFace" Sample code to accompany develop article
  16.         on techniques for embedding scripts in applications.
  17.  
  18. */
  19.  
  20.  
  21. #ifndef __STDIO__
  22. #include <StdIO.h>
  23. #endif
  24.  
  25. #ifndef __WINDOWOBJ__
  26. #include "WindowObj.h"
  27. #endif
  28.  
  29. #ifndef __SimpliFaceCOMMON__
  30. #include "SimpliFaceCommon.h"
  31. #endif
  32.  
  33. #ifndef __APPLICATIONCOMMON__
  34. #include "ApplicationCommon.h"
  35. #endif
  36.  
  37. #ifndef __MENUS__
  38. #include <Menus.h>
  39. #endif
  40. #ifndef __FONTS__
  41. #include <Fonts.h>
  42. #endif
  43. #ifndef __EVENTS__
  44. #include <Events.h>
  45. #endif
  46. #ifndef __WINDOWS__
  47. #include <Windows.h>
  48. #endif
  49. #ifndef __DIALOGS__
  50. #include <Dialogs.h>
  51. #endif
  52. #ifndef __QUICKDRAW__
  53. #include <Quickdraw.h>
  54. #endif
  55. #ifndef __MEMORY__
  56. #include <Memory.h>
  57. #endif
  58. #ifndef __RESOURCES__
  59. #include <Resources.h>
  60. #endif
  61. #ifndef __TOOLUTILS__
  62. #include <ToolUtils.h>
  63. #endif
  64. #ifndef __FILES__
  65. #include <Files.h>
  66. #endif
  67. #ifndef __STANDARDFILE__
  68. #include <StandardFile.h>
  69. #endif
  70. #ifndef __SYSEQU__
  71. #include <SysEqu.h>
  72. #endif
  73. #ifndef __PLSTRINGFUNCS__
  74. #include <PLStringFuncs.h>
  75. #endif
  76.  
  77. #ifndef __AEOBJECTS__
  78. #include <AEObjects.h>
  79. #endif
  80.  
  81. #ifndef __AERegistry__
  82. #include <AERegistry.h>
  83. #endif
  84. #ifndef __ASREGISTRY__
  85. #include <ASRegistry.h>
  86. #endif
  87.  
  88. #ifndef __AEOBJECTPACKING__
  89. #include <AEPackObject.h>
  90. #endif
  91.  
  92. #ifndef __AEOMTOKENS__
  93. #include "ObjModelTokens.h"
  94. #endif
  95. #ifndef __AEOMEVENTS__
  96. #include "ObjModelEvents.h"
  97. #endif
  98.  
  99. #ifndef __SCRIPTUTILS__
  100. #include "ScriptUtils.h"
  101. #endif
  102.  
  103.  
  104.  
  105. /**********************************************************************
  106. ** PUBLIC Constructor
  107. ***********************************************************************/
  108.  
  109. // when window object is first created, no window is opened;
  110. // (unless the window property 'visible' is initially true)
  111. // the window record is created and actually opened the first
  112. // time the window receives an 'open' event or is made 'visible'
  113.  
  114. TWindowObj::TWindowObj()
  115. {    
  116.     InitFields();
  117.     fInitialized = true;
  118. }
  119.  
  120. TWindowObj::TWindowObj(const AEDesc *theData)
  121. {    
  122.     InitFields();
  123.     SetData(theData);
  124.     fInitialized = true;
  125. }
  126.  
  127. TWindowObj::~TWindowObj()
  128. {
  129.     TListOfLongs    aList = fButtonElems;    // makes temporary copy of list
  130.     long             numElems = aList.CountElements();
  131.     while (numElems > 0)
  132.     {
  133.         delete (TInterfaceObj*)(aList.GetElement(numElems));
  134.         numElems--;
  135.     }
  136.     aList = fFieldElems;
  137.     numElems = aList.CountElements();
  138.     while (numElems > 0)
  139.     {
  140.         delete (TInterfaceObj*)(aList.GetElement(numElems));
  141.         numElems--;
  142.     }
  143.     fButtonElems.SetData(NULL);
  144.     fFieldElems.SetData(NULL);
  145.     
  146.     if (fWindowPtr)
  147.     {
  148.         CloseWindow(fWindowPtr);
  149.         fWindowPtr = NULL;
  150.     }
  151. }
  152.  
  153. void TWindowObj::InitFields(void)
  154. {
  155.     fInitialized = false;
  156.     fBounds.left = 100;
  157.     fBounds.top = 100;
  158.     fBounds.right = 200;
  159.     fBounds.bottom = 200;
  160.     fHasCloseBox = true;
  161.     fHasTitleBar = true;
  162.     fIsModal = false;
  163.     fIsResizable = false;
  164.     fIsZoomable = false;
  165.     fIsZoomed = false;
  166.     fName = "";
  167.     fShouldBeVisible = false;
  168.     fHasBeenCreated = false;
  169.     fWindowDefProc = documentProc;
  170.     fWindowPtr = NULL;
  171.     fButtonElems.SetData(NULL);
  172.     fFieldElems.SetData(NULL);
  173. }
  174.  
  175.  
  176. void TWindowObj::ActivateWindow(Boolean isActivating)
  177. {
  178. }
  179.  
  180.  
  181. void TWindowObj::UpdateWindow(void)
  182. {
  183.     if (fWindowPtr)
  184.     {
  185.         SetPort(fWindowPtr);
  186.         BeginUpdate(fWindowPtr);
  187.         DrawWindow();
  188.         EndUpdate(fWindowPtr);
  189.     }
  190. }
  191.  
  192.  
  193. void TWindowObj::DrawAllElements(DescType desiredClass)
  194. {
  195.     if (fWindowPtr)
  196.     {
  197.         TListOfLongs    aList;    // makes temporary copy of list
  198.         
  199.         if (desiredClass == cButton)
  200.             aList = fButtonElems;
  201.         else if (desiredClass == cTextField)
  202.             aList = fFieldElems;
  203.             
  204.         long numElems = aList.CountElements();
  205.         while (numElems > 0)
  206.         {
  207.             TInterfaceObj*    anElement = (TInterfaceObj*)(aList.GetElement(numElems));
  208.             if (anElement)
  209.                 anElement->DrawObject();
  210.             numElems--;
  211.         }
  212.     }
  213. }
  214.  
  215.  
  216. void TWindowObj::DrawWindow(void)
  217. {
  218.     if (fWindowPtr)
  219.     {
  220.         DrawAllElements(cTextField);
  221.         DrawAllElements(cButton);
  222.     }
  223. }
  224.  
  225.  
  226. /**********************************************************************
  227. ** PUBLIC AE Object Model support
  228. ***********************************************************************/
  229.  
  230.  
  231. OSErr TWindowObj::CountElements(DescType desiredClass, long *result)
  232. {
  233.     OSErr             err = errAEEventNotHandled;
  234.  
  235.     if (desiredClass == cButton)
  236.     {
  237.         *result = fButtonElems.CountElements();
  238.         err = 0;
  239.     }    
  240.     else if (desiredClass == cTextField)
  241.     {
  242.         *result = fFieldElems.CountElements();
  243.         err = 0;
  244.     }    
  245.     return err;
  246. }
  247.  
  248.                                     
  249. OSErr TWindowObj::CreateNewElement    (DescType desiredClass,
  250.                                         DescType position,
  251.                                         AEDesc *theData,
  252.                                         AERecord *theProperties,
  253.                                         TScriptableObject *theContainerObj,
  254.                                         TScriptableObject **theNewObj)
  255. {
  256.     OSErr             err = errAEEventNotHandled;
  257.     TInterfaceObj    *newInterfaceObj = NULL;
  258.         
  259.     if (desiredClass == cButton)
  260.     {
  261.         if (theProperties)
  262.             newInterfaceObj = new TButtonObj(this, theProperties);
  263.         else
  264.         {
  265.             newInterfaceObj = new TButtonObj(this);
  266.             if (newInterfaceObj && theData)
  267.                 newInterfaceObj->SetProperty(pName, theData);
  268.         }
  269.     }
  270.     else if (desiredClass == cTextField)
  271.     {
  272.         if (theProperties)
  273.             newInterfaceObj = new TLabelObj(this, theProperties);
  274.         else
  275.         {
  276.             newInterfaceObj = new TLabelObj(this);
  277.             if (newInterfaceObj && theData)
  278.                 newInterfaceObj->SetProperty(pName, theData);
  279.         }
  280.     }
  281.  
  282.     if (newInterfaceObj)
  283.     {
  284.         if (desiredClass == cButton)
  285.             fButtonElems.InsertElement((long)newInterfaceObj);
  286.         else
  287.             fFieldElems.InsertElement((long)newInterfaceObj);
  288.         *theNewObj = newInterfaceObj;
  289.         err = 0;
  290.     }
  291.  
  292.     return err;
  293. }    
  294.  
  295.  
  296. OSErr TWindowObj::ResolveContainer(TScriptableObject **theContainerObj)
  297. {
  298.     OSErr             err = 0;
  299.     
  300.     *theContainerObj = gSimpliFace;
  301.  
  302.     return err;
  303. }
  304.  
  305.                                     
  306. OSErr TWindowObj::ResolveElementByName(DescType desiredClass,
  307.                                         CStr255& nameStr,
  308.                                         TScriptableObject **theResultObj)
  309. {
  310.     OSErr           err = errAEEventNotHandled;
  311.     TInterfaceObj    *anObj = NULL;
  312.     CStr255         aName;
  313.     TListOfLongs    aList;    // makes temporary copy of list
  314.     
  315.     if (desiredClass == cButton)
  316.         aList = fButtonElems;
  317.     else if (desiredClass == cTextField)
  318.         aList = fFieldElems;
  319.     else
  320.         return err;
  321.         
  322.     long numElems = aList.CountElements();
  323.     while (numElems > 0)
  324.     {
  325.         anObj = (TInterfaceObj*)(aList.GetElement(numElems));
  326.         if (anObj)
  327.         {
  328.             anObj->GetName(aName);
  329.             if (aName == nameStr)
  330.             {
  331.                 *theResultObj = anObj;
  332.                 numElems = 0;
  333.                 err = 0;
  334.             }
  335.         }
  336.         numElems--;
  337.     }
  338.     
  339.     return err;
  340. }
  341.  
  342.                                     
  343. OSErr TWindowObj::ResolveElementByIndex(DescType desiredClass,
  344.                                         short theIndex,
  345.                                         TScriptableObject **theResultObj)
  346. {
  347.     OSErr           err = errAEEventNotHandled;
  348.     TInterfaceObj    *anObj = NULL;
  349.     CStr255         aName;
  350.     TListOfLongs    aList;    // makes temporary copy of list
  351.     short            index = theIndex;
  352.     
  353.     if (desiredClass == cButton)
  354.         aList = fButtonElems;
  355.     else if (desiredClass == cTextField)
  356.         aList = fFieldElems;
  357.     else
  358.         return err;
  359.         
  360.     if (index<0)
  361.     {
  362.         short numElems = (short)aList.CountElements();
  363.         index = numElems+index+1;
  364.     }
  365.         
  366.     *theResultObj = (TInterfaceObj*)(aList.GetElement(index));
  367.     if (*theResultObj != NULL)
  368.         err = 0;
  369.     
  370.     return err;
  371. }
  372.  
  373.  
  374.  
  375. OSErr TWindowObj::OpenObject(void)
  376. {
  377.     OSErr                 err = 0;
  378.     Boolean             isVisible = false;
  379.     
  380.     if (fWindowPtr)
  381.         isVisible = ((WindowPeek)fWindowPtr)->visible;
  382.     
  383.     if (fInitialized && !isVisible)
  384.     {
  385.         fShouldBeVisible = true;
  386.         if (!fWindowPtr)
  387.         {
  388.             Rect        theBounds = fBounds;
  389.             CStr255        theName = fName;
  390.             
  391.             fWindowPtr = NewWindow(NULL, &theBounds, theName, 
  392.                                     false, fWindowDefProc, WindowPtr(-1),
  393.                                     fHasCloseBox, (long)this);
  394.             
  395.         }
  396.         if (fWindowPtr)
  397.         {
  398.             ShowWindow(fWindowPtr);
  399.         }
  400.     }
  401.  
  402.     printf("TWindowObj::OpenObject(): err = %d\n", err);
  403.     return err;
  404. }
  405.  
  406. OSErr TWindowObj::CloseObject(void)
  407. {
  408.     OSErr                 err = 0;
  409.     Boolean             isVisible = false;
  410.     
  411.     if (fWindowPtr)
  412.         isVisible = ((WindowPeek)fWindowPtr)->visible;
  413.     if (fInitialized && isVisible)
  414.     {
  415.         fShouldBeVisible = false;
  416.         if (fWindowPtr)
  417.         {
  418.             HideWindow(fWindowPtr);
  419.         }
  420.     }
  421.  
  422.     return err;
  423. }
  424.                                     
  425. OSErr TWindowObj::GetProperty  (DescType propertyID, DescType wantType, AEDesc *result)
  426. {
  427.     OSErr                 err = errAEEventNotHandled;
  428.     Boolean             theBoolean;
  429.     
  430.     switch (propertyID)
  431.     {
  432.     case pBounds:
  433.         Rect theRect = fBounds;
  434.         if (fWindowPtr)
  435.             theRect = (*((WindowPeek)fWindowPtr)->strucRgn)->rgnBBox;
  436.         err = AECreateDesc(typeQDRectangle, (Ptr)&theRect,
  437.                             sizeof(theRect), result);
  438.         break;
  439.     case pName:
  440.         {
  441.             CStr255        theName = fName;
  442.             err = AECreateDesc(typeChar, (Ptr)&theName[1], 
  443.                                 theName.Length(), result);
  444.         }
  445.         break;
  446.     case pHasCloseBox:
  447.         theBoolean = fHasCloseBox;
  448.         err = AECreateDesc(typeBoolean, (Ptr)&theBoolean,
  449.                             sizeof(theBoolean), result);
  450.         break;
  451.     case pHasTitleBar:
  452.         theBoolean = fHasTitleBar;
  453.         err = AECreateDesc(typeBoolean, (Ptr)&theBoolean,
  454.                             sizeof(theBoolean), result);
  455.         break;
  456.     case pIsModal:
  457.         theBoolean = fIsModal;
  458.         err = AECreateDesc(typeBoolean, (Ptr)&theBoolean,
  459.                             sizeof(theBoolean), result);
  460.         break;
  461.     case pIsResizable:
  462.         theBoolean = fIsResizable;
  463.         err = AECreateDesc(typeBoolean, (Ptr)&theBoolean,
  464.                             sizeof(theBoolean), result);
  465.         break;
  466.     case pIsZoomable:
  467.         theBoolean = fIsZoomable;
  468.         err = AECreateDesc(typeBoolean, (Ptr)&theBoolean,
  469.                             sizeof(theBoolean), result);
  470.         break;
  471.     case pIsZoomed:
  472.         theBoolean = fIsZoomed;
  473.         err = AECreateDesc(typeBoolean, (Ptr)&theBoolean,
  474.                             sizeof(theBoolean), result);
  475.         break;
  476.     case pIndex:
  477.         short theIndex = 0;
  478.         if (fWindowPtr)
  479.         {
  480.             do 
  481.                 theIndex++;
  482.             while (fWindowPtr != GetWindowPtrOfNthWindow(theIndex));
  483.         }
  484.         err = AECreateDesc(typeShortInteger, (Ptr)&theIndex,
  485.                             sizeof(theIndex), result);
  486.         break;
  487.     case pVisible:
  488.         theBoolean = false;
  489.         if (fWindowPtr)
  490.             theBoolean = ((WindowPeek)fWindowPtr)->visible;
  491.         err = AECreateDesc(typeBoolean, (Ptr)&theBoolean,
  492.                             sizeof(theBoolean), result);
  493.         break;
  494.     default:
  495.         err = TScriptableObject::GetProperty(propertyID, wantType, result);
  496.         break;
  497.     }
  498.     
  499.     return err;
  500. }
  501.                                             
  502. OSErr TWindowObj::SetProperty  (DescType propertyID, const AEDesc *theData)
  503. {
  504.     OSErr                 err = errAEEventNotHandled;
  505.     
  506.     if (fWindowPtr && (propertyID == pHasCloseBox ||
  507.                          propertyID == pHasTitleBar ||
  508.                          propertyID == pIsModal ||
  509.                          propertyID == pIsResizable ||
  510.                          propertyID == pIsZoomable))
  511.         return errAENotModifiable;
  512.         
  513.     switch (propertyID)
  514.     {
  515.     case pBounds:
  516.         {
  517.             Rect    newBounds;
  518.             err = GetRectFromDescriptor(theData, &newBounds);
  519.             if (!err)
  520.             {
  521.                 fBounds = newBounds;
  522.                 if (fInitialized && fWindowPtr)
  523.                 {
  524.                     MoveWindow(fWindowPtr, newBounds.left, newBounds.top, false);
  525.                     SizeWindow(fWindowPtr, newBounds.right-newBounds.left, 
  526.                                 newBounds.bottom-newBounds.top, true);
  527.                 }
  528.             }
  529.         }
  530.         break;
  531.     case pName:
  532.         {
  533.             CStr255        theName;
  534.             err = GetCStringFromDescriptor(theData, &theName);
  535.             if (!err)
  536.             {
  537.                 fName = theName;
  538.                 if (fInitialized && fWindowPtr)
  539.                     SetWTitle(fWindowPtr, (ConstStr255Param) theName);
  540.             }
  541.         }
  542.         break;
  543.     case pVisible:
  544.         Boolean        theBoolean = false;
  545.         err = GetBooleanFromDescriptor(theData, &theBoolean);
  546.         if (!err)
  547.         {
  548.             fShouldBeVisible = theBoolean;
  549.             if (fInitialized)
  550.             {
  551.                 if (fShouldBeVisible)
  552.                     OpenObject();
  553.                 else
  554.                     CloseObject();
  555.             }
  556.         }
  557.         break;
  558.     default:
  559.         err = TScriptableObject::SetProperty(propertyID, theData);
  560.         break;
  561.     }
  562.     
  563.     return err;
  564. }    
  565.  
  566.  
  567.  
  568. OSErr TWindowObj::GetObjectSpecifier  (AEDesc *result)
  569. {
  570.     // walk along containment hierarchy & build object specifier 
  571.     //
  572.     // top container is null container
  573.     // then remainder are the object classes specified by name
  574.     //
  575.     OSErr         err = 0;
  576.     AEDesc        containerDesc, newContainerDesc;
  577.     
  578.     err = MakeNullDesc(&containerDesc);
  579.     if (!err)
  580.     {
  581.         AEDesc        nameDesc;
  582.         
  583.         err = MakeNameDesc(fName, &nameDesc);
  584.         if (!err)
  585.         {
  586.             err = CreateObjSpecifier(cWindow, &containerDesc, formName, 
  587.                                      &nameDesc, true, &newContainerDesc);
  588.             containerDesc = newContainerDesc;
  589.         }
  590.     }
  591.     
  592.     *result = containerDesc;
  593.         
  594.     return err;
  595. }    
  596.  
  597.  
  598. TScriptableObject* TWindowObj::FindElement(Point& thePt, DescType desiredClass)
  599. {
  600.     TListOfLongs    aList;    // makes temporary copy of list
  601.     
  602.     if (desiredClass == cButton)
  603.         aList = fButtonElems;
  604.     else if (desiredClass == cTextField)
  605.         aList = fFieldElems;
  606.         
  607.     long numElems = aList.CountElements();
  608.     while (numElems > 0)
  609.     {
  610.         TInterfaceObj*    anElement = (TInterfaceObj*)(aList.GetElement(numElems));
  611.         if (anElement)
  612.         {
  613.             Rect    theBounds;
  614.             
  615.             anElement->GetBounds(theBounds);
  616.             
  617.             if (PtInRect(thePt, &theBounds))
  618.                 return anElement;
  619.         }
  620.         numElems--;
  621.     }
  622.     return NULL;
  623. }
  624.  
  625.  
  626. OSErr TWindowObj::GetTargetObjectSpecifier  (EventRecord& theEvent, AEDesc *result)
  627. {
  628.     OSErr         err = 0;
  629.     AEDesc        containerDesc;
  630.     
  631.     if (theEvent.what == mouseDown)
  632.     {
  633.         Point    clickPt = theEvent.where;
  634.         
  635.         GlobalToLocal(&clickPt);
  636.         
  637.         TInterfaceObj* theElement = (TInterfaceObj*)FindElement(clickPt, cButton);
  638.         if (!theElement)
  639.             theElement = (TInterfaceObj*)FindElement(clickPt, cTextField);
  640.             
  641.         if (theElement)
  642.             err = theElement->GetObjectSpecifier(&containerDesc);
  643.         else
  644.             err = GetObjectSpecifier(&containerDesc);
  645.     }
  646.     else
  647.         err = GetObjectSpecifier(&containerDesc);
  648.         
  649.     *result = containerDesc;
  650.         
  651.     return err;
  652. }    
  653.  
  654.  
  655.  
  656. /**********************************************************************
  657. ** PUBLIC Constructor
  658. ***********************************************************************/
  659.  
  660. TInterfaceObj::TInterfaceObj(TWindowObj* container)
  661. {    
  662.     fContainer = container;
  663.     InitFields();
  664.     fInitialized = true;
  665. }
  666.  
  667. TInterfaceObj::TInterfaceObj(TWindowObj* container, const AEDesc *theData)
  668. {    
  669.     fContainer = container;
  670.     InitFields();
  671.     SetData(theData);
  672.     fInitialized = true;
  673. }
  674.  
  675. TInterfaceObj::~TInterfaceObj()
  676. {
  677. }
  678.  
  679. void TInterfaceObj::InitFields(void)
  680. {
  681.     Str255        fontName;
  682.     
  683.     fInitialized = false;
  684.     fBounds.left = 0;
  685.     fBounds.top = 0;
  686.     fBounds.right = 0;
  687.     fBounds.bottom = 0;
  688.     GetFontName(applFont, fontName);
  689.     fFontName = fontName;
  690.     fName = "";
  691.     fFontSize = 12;
  692.     fFontStyle = 0;
  693.     fFgColor.red = 0xFFFF;
  694.     fFgColor.green = 0xFFFF;
  695.     fFgColor.blue = 0xFFFF;
  696. }
  697.  
  698.  
  699. DescType TInterfaceObj::GetClass(void)
  700. {
  701.     return typeNull;
  702. }
  703.  
  704.  
  705. void TInterfaceObj::DrawObject(void)
  706. {
  707. }
  708.  
  709.  
  710. void TInterfaceObj::ForceRedrawObject(Boolean drawNow)
  711. {
  712.     if (drawNow)
  713.         DrawObject();
  714.     else
  715.     {
  716.         Rect theBounds = fBounds;
  717.         InvalRect(&theBounds);
  718.     }
  719. }
  720.  
  721.  
  722. OSErr TInterfaceObj::ResolveContainer(TScriptableObject **theContainerObj)
  723. {
  724.     OSErr             err = 0;
  725.     
  726.     *theContainerObj = fContainer;
  727.  
  728.     return err;
  729. }
  730.  
  731.                                     
  732. OSErr TInterfaceObj::GetProperty  (DescType propertyID, DescType wantType, AEDesc *result)
  733. {
  734.     OSErr                 err = errAEEventNotHandled;
  735.     
  736.     switch (propertyID)
  737.     {
  738.     case pBounds:
  739.         {
  740.             Rect theRect = fBounds;
  741.             err = AECreateDesc(typeQDRectangle, (Ptr)&theRect,
  742.                                 sizeof(theRect), result);
  743.         }
  744.         break;
  745.     case pPointSize:
  746.         {
  747.             short theNum = fFontSize;
  748.             err = AECreateDesc(typeShortInteger, (Ptr)&theNum,
  749.                                 sizeof(theNum), result);
  750.         }
  751.         break;
  752.     case pFont:
  753.         {
  754.             CStr255    theName = fFontName;
  755.             err = AECreateDesc(typeChar, (Ptr)&theName[1], 
  756.                                 theName.Length(), result);
  757.         }
  758.         break;
  759.     case pName:
  760.         {
  761.             CStr255    theName = fName;
  762.             err = AECreateDesc(typeChar, (Ptr)&theName[1], 
  763.                                 theName.Length(), result);
  764.         }
  765.         break;
  766.     default:
  767.         err = TScriptableObject::GetProperty(propertyID, wantType, result);
  768.         break;
  769.     }
  770.     
  771.     return err;
  772. }
  773.                                             
  774. OSErr TInterfaceObj::SetProperty  (DescType propertyID, const AEDesc *theData)
  775. {
  776.     OSErr                 err = errAEEventNotHandled;
  777.     
  778.     switch (propertyID)
  779.     {
  780.     case pBounds:
  781.         {
  782.             Rect    newBounds;
  783.             err = GetRectFromDescriptor(theData, &newBounds);
  784.             if (!err)
  785.             {
  786.                 ForceRedrawObject(false);
  787.                 fBounds = newBounds;
  788.                 ForceRedrawObject(false);
  789.             }
  790.         }
  791.         break;
  792.     case pFont:
  793.         {
  794.             CStr255        theName;
  795.             err = GetCStringFromDescriptor(theData, &theName);
  796.             if (!err)
  797.             {
  798.                 fFontName = theName;
  799.                 ForceRedrawObject(false);
  800.             }
  801.         }
  802.         break;
  803.     case pName:
  804.         {
  805.             CStr255        theName;
  806.             err = GetCStringFromDescriptor(theData, &theName);
  807.             if (!err)
  808.             {
  809.                 fName = theName;
  810.                 ForceRedrawObject(false);
  811.             }
  812.         }
  813.         break;
  814.     case pPointSize:
  815.         {
  816.             short        theNum = 0;
  817.             err = GetIntegerFromDescriptor(theData, &theNum);
  818.             if (!err)
  819.             {
  820.                 fFontSize = theNum;
  821.                 ForceRedrawObject(false);
  822.             }
  823.         }
  824.         break;
  825.     default:
  826.         err = TScriptableObject::SetProperty(propertyID, theData);
  827.         break;
  828.     }
  829.     
  830.     return err;
  831. }    
  832.  
  833.  
  834.  
  835.  
  836. OSErr TInterfaceObj::GetObjectSpecifier  (AEDesc *result)
  837. {
  838.     // walk along containment hierarchy & build object specifier 
  839.     //
  840.     // top container is null container
  841.     // then remainder are the object classes specified by name
  842.     //
  843.     OSErr         err = 0;
  844.     AEDesc        containerDesc, newContainerDesc;
  845.     
  846.     err = fContainer->GetObjectSpecifier(&containerDesc);
  847.     if (!err)
  848.     {
  849.         AEDesc        nameDesc;
  850.         
  851.         err = MakeNameDesc(fName, &nameDesc);
  852.         if (!err)
  853.         {
  854.             err = CreateObjSpecifier(GetClass(), &containerDesc, formName, 
  855.                                      &nameDesc, true, &newContainerDesc);
  856.             containerDesc = newContainerDesc;
  857.         }
  858.     }
  859.     
  860.     *result = containerDesc;
  861.         
  862.     return err;
  863. }    
  864.  
  865.  
  866.  
  867.  
  868. /**********************************************************************
  869. ** PUBLIC Constructor
  870. ***********************************************************************/
  871.  
  872. TLabelObj::TLabelObj(TWindowObj* container) : TInterfaceObj (container)
  873. {    
  874.     InitFields();
  875. }
  876.  
  877. TLabelObj::TLabelObj(TWindowObj* container,
  878.                 const AEDesc *theData) : TInterfaceObj (container)
  879. {    
  880.     InitFields();
  881.     SetData(theData);
  882. }
  883.  
  884. TLabelObj::~TLabelObj()
  885. {
  886.     if (fContents)
  887.         DisposHandle(fContents);
  888. }
  889.  
  890.  
  891. void TLabelObj::InitFields(void)
  892. {
  893.     fContents = NULL;
  894. }
  895.  
  896.  
  897. DescType TLabelObj::GetClass(void)
  898. {
  899.     return cTextField;
  900. }
  901.  
  902.  
  903. void TLabelObj::DrawObject(void)
  904. {
  905.     Handle        datah = fContents;
  906.     
  907.     if (fInitialized && datah)
  908.     {
  909.         Rect        theBounds = fBounds;
  910.         
  911.         InsetRect(&theBounds, 3, 3);    // standard 3pt inset for text fields
  912.         
  913.         if (!EmptyRect(&theBounds))
  914.         {
  915.             HLock(datah);
  916.             TextBox(StripAddress((Ptr)(*datah)), GetHandleSize(datah), 
  917.                     &theBounds, teJustLeft);
  918.             HUnlock(datah);
  919.         }
  920.     }
  921. }
  922.  
  923.  
  924. void TLabelObj::ForceRedrawObject(Boolean drawNow)
  925. {
  926.     TInterfaceObj::ForceRedrawObject(drawNow);
  927. }
  928.  
  929.  
  930.  
  931. OSErr TLabelObj::GetProperty  (DescType propertyID, DescType wantType, AEDesc *result)
  932. {
  933.     OSErr                 err = errAEEventNotHandled;
  934.     
  935.     switch (propertyID)
  936.     {
  937.     case pContents:
  938.         Handle     h = fContents;
  939.         err = HandToHand(&h);
  940.         if (!err)
  941.         {
  942.             result->descriptorType = typeChar;
  943.             result->dataHandle = h;
  944.         }
  945.         break;
  946.     default:
  947.         err = TInterfaceObj::GetProperty(propertyID, wantType, result);
  948.         break;
  949.     }
  950.     
  951.     return err;
  952. }
  953.                                             
  954. OSErr TLabelObj::SetProperty  (DescType propertyID, const AEDesc *theData)
  955. {
  956.     OSAError            err = errAEEventNotHandled;
  957.     
  958.     switch (propertyID)
  959.     {
  960.     case pContents:
  961.         {
  962.             AEDesc        displayData;
  963.             OSAID        valueID = kOSANullScript;
  964.             
  965.             err = OSACoerceFromDesc(gScriptingComponent, theData,
  966.                                     kOSAModeNull, &valueID);
  967.                                     
  968.             if (!err && valueID != kOSANullScript)
  969.                 err = OSADisplay(gScriptingComponent, valueID, 
  970.                                     typeChar, kOSAModeDisplayForHumans, 
  971.                                     &displayData);
  972.     
  973.             OSADispose(gScriptingComponent, valueID);
  974.             
  975.             if (!err && displayData.dataHandle)
  976.             {
  977.                 Handle     h = displayData.dataHandle;
  978.                 err = HandToHand(&h);
  979.                 if (fContents)
  980.                     DisposHandle(fContents);
  981.                 fContents = h;
  982.                 AEDisposeDesc(&displayData);
  983.                 ForceRedrawObject(false);
  984.             }
  985.             else
  986.                 printf("Failed to set contents of field: err=%ld\n", err);
  987.         }
  988.         break;
  989.     default:
  990.         err = TInterfaceObj::SetProperty(propertyID, theData);
  991.         break;
  992.     }
  993.     
  994.     return (OSErr)err;
  995. }    
  996.  
  997.  
  998. /**********************************************************************
  999. ** PUBLIC Constructor
  1000. ***********************************************************************/
  1001.  
  1002. TButtonObj::TButtonObj(TWindowObj* container) : TInterfaceObj (container)
  1003. {    
  1004.     InitFields();
  1005. }
  1006.  
  1007. TButtonObj::TButtonObj(TWindowObj* container, 
  1008.                 const AEDesc *theData) : TInterfaceObj (container)
  1009. {    
  1010.     InitFields();
  1011.     SetData(theData);
  1012. }
  1013.  
  1014. TButtonObj::~TButtonObj()
  1015. {
  1016.     if (fControlH)
  1017.         DisposeControl(fControlH); 
  1018. }
  1019.  
  1020. void TButtonObj::InitFields(void)
  1021. {
  1022.     TInterfaceObj::InitFields();
  1023.     
  1024.     fButtonKind = kAEBtnStandard;
  1025.     fControlH = NULL;
  1026. }
  1027.  
  1028.  
  1029. DescType TButtonObj::GetClass(void)
  1030. {
  1031.     return cButton;
  1032. }
  1033.  
  1034.  
  1035. void TButtonObj::DrawObject(void)
  1036. {
  1037.     if (!fControlH)
  1038.     {
  1039.         // create the button
  1040.         Rect        theBounds = fBounds;
  1041.         CStr255        title = fName;
  1042.         short        theProcID = pushButProc;    // assume standard by default
  1043.         WindowPtr    theWindow = fContainer->GetWindowPtr();
  1044.         
  1045.         if (fButtonKind == kAEBtnCheckbox)
  1046.             theProcID = checkBoxProc;
  1047.         else if (fButtonKind == kAEBtnCheckbox)
  1048.             theProcID = radioButProc;
  1049.         
  1050.         fControlH = NewControl(theWindow, &theBounds, title, true,
  1051.                                 0, 0, 1, theProcID, (long)this);
  1052.     }
  1053.     if (fControlH)
  1054.         Draw1Control(fControlH);
  1055. }
  1056.  
  1057.  
  1058. void TButtonObj::ForceRedrawObject(Boolean drawNow)
  1059. {
  1060.     TInterfaceObj::ForceRedrawObject(drawNow);
  1061. }
  1062.  
  1063.  
  1064. OSErr TButtonObj::GetProperty  (DescType propertyID, DescType wantType, AEDesc *result)
  1065. {
  1066.     OSErr                 err = errAEEventNotHandled;
  1067.     
  1068.     switch (propertyID)
  1069.     {
  1070.     case pButtonKind:
  1071.         DescType theKind = fButtonKind;
  1072.         err = AECreateDesc(typeType, (Ptr)&theKind, sizeof(theKind), result);
  1073.         break;
  1074.     default:
  1075.         err = TInterfaceObj::GetProperty(propertyID, wantType, result);
  1076.         break;
  1077.     }
  1078.     
  1079.     return err;
  1080. }
  1081.                                             
  1082. OSErr TButtonObj::SetProperty  (DescType propertyID, const AEDesc *theData)
  1083. {
  1084.     OSErr                 err = errAEEventNotHandled;
  1085.     
  1086.     switch (propertyID)
  1087.     {
  1088.     case pBounds:
  1089.         err = TInterfaceObj::SetProperty(propertyID, theData);
  1090.         if (!err && fControlH)
  1091.         {
  1092.             Rect newBounds = fBounds;
  1093.             MoveControl(fControlH, newBounds.left, newBounds.top);
  1094.             SizeControl(fControlH, newBounds.right-newBounds.left, 
  1095.                         newBounds.bottom-newBounds.top);
  1096.         }
  1097.         break;
  1098.     case pButtonKind:
  1099.         DescType theKind;
  1100.         err = GetDescTypeFromDescriptor(theData, &theKind);
  1101.         if (!err)
  1102.             fButtonKind = theKind;
  1103.         break;
  1104.     default:
  1105.         err = TInterfaceObj::SetProperty(propertyID, theData);
  1106.         break;
  1107.     }
  1108.         
  1109.     return err;
  1110. }    
  1111.